`

The shebang line can also take optional arguments to change

how the script executes. For example, you could pass the special

argument -x to your bash shebang, like so:

#!/bin/bash -x

This option will print all commands and their arguments as they

are executed to the terminal. It is useful for debugging scripts as you

are developing them.

Another example of an optional argument is -r:

#!/bin/bash -r

This optional argument will create a restricted bash shell, which

restricts certain potentially dangerous commands that could, for

example, navigate to certain directories, change sensitive

environment variables, or attempt to turn off the restricted shell from

within the script.

Specifying an argument within the shebang line requires

modifying the script, but you can also pass arguments to the bash

interpreter using the syntax in Listing 1-5.

$ bash -r myscript.sh

Listing 1-5

Passing an argument to bash

Whether you pass arguments to the bash interpreter on the

command line or on the shebang line wont make a difference. The

command line option is just an easier way to trigger different modes.

Comments

Comments are parts of a script that the bash interpreter won’t

treat as code, and they can improve the readability of a program.

Imagine that you write a long script and, a few years later, need to

modify some of its logic. If you didn’t write comments to explain

what you did, you might find it quite challenging to remember the

purpose of each section.

Comments in bash start with a pound sign (#), as shown in

Listing 1-6.

#!/bin/bash

# This is my first script.

Listing 1-6

A comment in a bash script

Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks